package com.situ.blog.servlet;

import com.situ.blog.pojo.Auth;
import com.situ.blog.pojo.query.AuthQuery;
import com.situ.blog.service.IAuthService;
import com.situ.blog.service.impl.AuthserviceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.MyBeanUtil;
import com.situ.blog.util.PageRusult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/auth")
public class AuthServlet extends HttpServlet {
    IAuthService authService = new AuthserviceImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method==null||method.equals("")){
            method = "selectByPage";
        }
        switch (method){
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            /*case "selectById":
                selectById(req,resp);
                break;*/
            case "update":
                update(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
        }
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.deleteAll");
        //传过来的是一个id的数组
        String[] ids = req.getParameterValues("ids[]");
        System.out.println(ids);
        //收到了传过来的id值，然后调用service
        authService.deleteAll(ids);
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        authService.updateStatus(id,Integer.parseInt(status));
        JSONUtil.toJSON(resp,Result.ok("更新成功"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.update");
        Auth auth = MyBeanUtil.copyToBean(req, Auth.class);
        authService.update(auth);
        Result result = new Result(0,"修改成功");
        JSONUtil.toJSON(resp,result);
    }

    /*private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.selectById");
        String id = req.getParameter("id");
        List<AuthType> list = authTypeService.selectAll();
        Auth auth = authService.selectById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("auth",auth);
        System.out.println("servlet中:"+auth+"and list:"+list);
        JSONUtil.toJSON(resp,Result.ok(map));

    }*/

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.add");
        String name = req.getParameter("name");
        System.out.println(name);
        //封装一下

        Auth auth = MyBeanUtil.copyToBean(req, Auth.class);
        System.out.println(auth);
        authService.add(auth);
        //返回Ajax数据
        Result result = new Result(0,"添加成功");
        JSONUtil.toJSON(resp,result);
    }


    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("AuthServlet.deleteById");
        //接收参数
        String id = req.getParameter("id");
        System.out.println("id:"+id);
        //调用函数
        int count = authService.deleteById(Integer.parseInt(id));//没有返回值
        Result result = null;
        if (count>0){
            result = new Result(0,"删除成功");
        }else {
            result = new Result(1,"删除失败");
        }
        //固定的三行
        JSONUtil.toJSON(resp,result);
        //*resp.setContentType("text/html;charset=UTF-8");
        //ObjectMapper mapper = new ObjectMapper();
        //mapper.writeValue(resp.getWriter(), result);//*//现在不知道这里要返回什么
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("AuthServlet.selectByPage");
        /*String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");*/
        //1.封装所需数据page:当前页码 ;limit:每页的信息数 ;name:查找的课程名字 ;credit:查找的学分

        //调用位于service层的分页查找功能
        AuthQuery authQuery = MyBeanUtil.copyToBean(req, AuthQuery.class);
        System.out.println(authQuery);
        PageRusult pageResult = authService.selectByPage(authQuery);

        JSONUtil.toJSON(resp, pageResult);

    }
}
